//
//  TopCollectionView.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/18.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "TopCollectionView.h"
#import "BananerCollectionViewCell.h"
#import "ActiveCollectionViewCell.h"
#import "BannerModel.h"
#import "CategoryModel.h"

@interface TopCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;
@property (nonatomic, strong)ActiveCollectionViewCell *cell;
@property (nonatomic, strong)NSArray<BannerModel *> * bannerArray;
@property (nonatomic, strong)NSArray<CategoryModel *> * categoryArray;
@property (nonatomic, strong)NSIndexPath * indexPath;
//@property (nonatomic, assign)int banaerCount;
//@property (nonatomic, assign)int categoryCount;

@end

@implementation TopCollectionView

- (void)setType:(NSString *)type{
    
    _type = type;
    //该方法也可以设置itemSize
    if ([self.type isEqualToString:@"bana"]) {
        
        self.layout.itemSize =CGSizeMake(205 * SCREENH6, 166 * SCREENH6);
        self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 15 * SCREENH6);
    }else{
        
        self.layout.itemSize =CGSizeMake(63 * SCREENH6, 90 * SCREENH6);
        self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        self.bannerArray = [NSArray bg_arrayWithName:@"banner"];
        self.categoryArray = [NSArray bg_arrayWithName:@"category"];
        [self loadData];
        [self setupUI];
    }
    
    return self;
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getBannerResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.bannerArray = [NSArray yy_modelArrayWithClass:[BannerModel class] json:responseData[@"data"]];
            
//            [self.collectionView reloadData];
//            if (self.banaerCount == 1) {
            
                NSArray* testResult = [NSArray bg_arrayWithName:@"banner"];
                if (testResult.count > 0) {
                    
                    [NSArray bg_clearArrayWithName:@"banner"];
                    [self.bannerArray bg_saveArrayWithName:@"banner"];
                }else{
                    
                    [self.bannerArray bg_saveArrayWithName:@"banner"];
                }
//            }
            
//            self.banaerCount = self.banaerCount + 1;
        }else{
            
        }
    }];
    
    [CategoryModel getCategoryWithSucess:^(NSArray<CategoryModel *> *array) {
        
        self.categoryArray = array;
        
//        if (self.categoryCount == 1) {
        
            NSArray* testResult = [NSArray bg_arrayWithName:@"category"];
            if (testResult.count > 0) {
                
                [NSArray bg_clearArrayWithName:@"category"];
                [self.categoryArray bg_saveArrayWithName:@"category"];
            }else{
                
                [self.categoryArray bg_saveArrayWithName:@"category"];
            }
//        }
        
//        self.categoryCount = self.categoryCount + 1;
    } error:^{
        
    }];
    
    
}


- (void)setCategoryArray:(NSArray<CategoryModel *> *)categoryArray{
    
    _categoryArray = categoryArray;
    
    [self.collectionView reloadData];
}


- (void)setBannerArray:(NSArray<BannerModel *> *)bannerArray{
    
    _bannerArray = bannerArray;
    
    [self.collectionView reloadData];
}

- (void)setupUI{
    
    self.backgroundColor = ZRWhiteColor;
    
    //1.初始化layout
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    [self.layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
    
    collectionView.backgroundColor = ZRWhiteColor;
    collectionView.showsHorizontalScrollIndicator = NO;
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[BananerCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [collectionView registerClass:[ActiveCollectionViewCell class] forCellWithReuseIdentifier:@"ActiveCellId"];
    //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
//    [collectionView setContentOffset:CGPointMake(-30 * SCREENH6, 0)];
    
    self.collectionView = collectionView;
    
    [self addSubview:self.collectionView];
}


#pragma mark collectionView代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if ([self.type isEqualToString:@"bana"]) {
        
        return self.bannerArray.count;
    }else{
        
        return self.categoryArray.count;
    }
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.type isEqualToString:@"bana"]) {
        
        BananerCollectionViewCell *cell = (BananerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
        //设置数据
        cell.model = self.bannerArray[indexPath.row];
        
        return cell;
    }else{
        
        self.cell = (ActiveCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ActiveCellId" forIndexPath:indexPath];
        //设置数据
        
        CategoryModel * model = self.categoryArray[indexPath.row];
        
//        [self.cell.imageButton setBackgroundColor:ZRBlackColor];
        [self.cell.backView setBackgroundColor:ZRBlackColor];

        if (!self.indexPath) {
            
            
            if (indexPath.row == 0) {
                
                self.indexPath = indexPath;
                
//                [self.cell.imageButton setBackgroundColor:[UIColor yellowColor]];
                [self.cell.backView setBackgroundColor:ColorWithRGB(0xFFF3AE)];
                
                NSDictionary * dict = @{@"ID":model.ID,@"type":@"first"};
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeGoodsListData" object:nil userInfo:dict];
            }
        }else{
            
            if (self.indexPath == indexPath) {
                
//               self.cell.imageButton.backgroundColor = [UIColor yellowColor];
                [self.cell.backView setBackgroundColor:ColorWithRGB(0xFFF3AE)];
            }
            
           
        }
        
//        self.cell.tag = indexPath.row;
        
        self.cell.model = model;
        
        return self.cell;
    }
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self.type isEqualToString:@"active"]) {
        
        self.indexPath = indexPath;
        
        [self.collectionView reloadData];
        
        CategoryModel * model = self.categoryArray[indexPath.row];
        
        NSDictionary * dict = @{@"ID":model.ID,@"type":@"more"};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeGoodsListData" object:nil userInfo:dict];
    }else{
        
        BannerModel * model = self.bannerArray[indexPath.row];
        
        NSDictionary * dict = @{@"model":model};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToBannerView" object:nil userInfo:dict];
    }
}


- (void)clickItemButton:(UIButton *)button{
    

}

@end
